package com.cn.zkc2;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

import java.util.List;

/**
 * Created by chenyuncong on 2019/4/1.
 */
public class ZKClientTest {
    /** zookeeper地址 */
    static final String connectString = "127.0.0.1:2181";
    /** session超时时间 */
    static final int sessionTimeout = 5000;

    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(connectString), 10000);

        // 创建一个临时节点，没有值或者值为null
        zkClient.createEphemeral("/temp");
        // 递归创建一个节点
        zkClient.createPersistent("/super/c1/c2", true);
        // 给节点赋值
        zkClient.writeData("/super", "super");
        zkClient.writeData("/super/c1", "super c1");
        zkClient.writeData("/super/c1/c2", "super c1 c2");

        // 创建一个节点并赋值(该操作不支持创建一个递归路径并赋值)
        zkClient.createPersistent("/test", "test");


        List<String> children = zkClient.getChildren("/super");
        children.forEach(path -> {
            System.out.println(path);
            String fullPath = "/super/" + path;
            String data = zkClient.readData(fullPath);
            System.out.println(fullPath + "=" + data);

        });

        boolean exists = zkClient.exists("/super");
        System.out.println(exists);

        zkClient.deleteRecursive("/super");
        zkClient.deleteRecursive("/test");

        Thread.sleep(10000);
        zkClient.close();
    }
}
